<?php
/**
 * @version 	$Id: fConfig.class.php 82 2010-03-17 08:45:39Z mylifeisskidrow@gmail.com $
 * @author 		Luu Trong Hieu<tronghieu1012@yahoo.com>
 * @copyright 	Copyright (c) 2010 Luu Trong Hieu. All right reserved.
 * @license 	GNU/GPL see license.php
 */
defined('IN_FLYWHEEL') or die('Restriced access!');

/**
 * Config
 * 
 * @package 	system
 * @subpackage 	config
 *
 */

class fConfig {	
	protected static $_data = array();
	/**
	 * Set Config
	 *
	 * @param string	$name 	config name
	 * @param string	$value	config value
	 * @param string	$namespace config namespace, default = 'default'
	 */
	public static function set($name, $value, $namespace = 'default') {
		self::$_data[$namespace][$name] = $value;		
	}
	
	/**
	 * Get config
	 *
	 * @param string	$name		config name
	 * @param string	$namespace	config namespace, default ='default'
	 * @return mixed	config value
	 */
	public static function get($name, $namespace = 'default') {
        $deep = explode('/', $name);
        
        $data = self::$_data[$namespace][$deep[0]];        
        $i = 0;
        while ($i < (sizeof($deep)-1)) {
            ++$i;
            $data = $data[$deep[$i]];            
        }
        return $data;
//		return self::$_data[$namespace][$name];
	}
	
	/**
	 * Has config
	 *
	 * @param string	 $name		config name
	 * @param string 	$namespace	config namespace
	 * @return boolean. true if has config.
	 */
	public static function has($name, $namespace = 'default') {
		if ($name == null) {
			return isset(self::$_data[$namespace]);
		}
		return isset(self::$_data[$namespace][$name]);
	}
	
	/**
	 * Add
	 *
	 * @param array		$params	An associative array of 
	 * 							config parameters and their associated values
	 * @param string	$namespace
	 */
	public static function add($params, $namespace = 'default') {
		if (!isset(self::$_data[$namespace])) {
			self::$_data[$namespace] = array();
		}
		self::$_data[$namespace] = array_merge(self::$_data[$namespace], $params);		
	}
	
	/**
	 * Get Config By Namespace
	 *
	 * @param string $namespace
	 * @return array
	 */
	public static function getConfigByNamespace($namespace = 'default') {
		return self::$_data[$namespace];		
	}
	
	/**
	 * Get All Config
	 *
	 * @return array
	 */
	public static function getAll() {
		return self::$_data;
	}
	
	/**
	 * Clear config
	 *
	 * @param mixed $namespace. method clean all config if $namespace = null
	 * @return void
	 */
	public static function clear($namespace = null) {
		if ($namespace == null) {
			self::$_data = array();		
		}
		else if (isset(self::$_data[$namespace])){
			self::$_data[$namespace] = array();
		}
	}
	
	
	/**
	 * Load
	 *
	 * @param string $file
	 * @param string $namespace
	 * @param boolean $return
	 * @param boolean $write
	 * @return config array if $return = true
	 * @throws fConfigException
	 */
	public static function load($file, $namespace = 'default', $write = true) {
		if (!file_exists($file)) 
			throw new fConfigException('File config: "'. $file .'" does not exists!');
		if (fConfig::get('environment') == fApplication::ENV_DEV) {
			$config = parse_ini_file($file, true);
		}
		else {
			$configCacheFileName = basename($file, '.ini');
			$configCachePath = _CACHE_PATH_.fFactory::getApplication()->getName()._DS_.'config';
			fFolderHelper::create($configCachePath);
	
			if (!file_exists($configCachePath._DS_.$configCacheFileName.'.php')) {
				$config = parse_ini_file($file, true);
				if ($write) {
					$handle = self::configHandle($config, $configCacheFileName);								
					file_put_contents($configCachePath._DS_.$configCacheFileName.'.php', $handle);
				}
			}
			else {
				$config = require $configCachePath._DS_.$configCacheFileName.'.php';															
			}
		}

		self::add($config, $namespace);
	}
	
	/**
	 * Config Handle
	 *
	 * @param array	 $data	config array handle
	 * @param string $file	config file
	 * @return data to be write cache file
	 */
	private function configHandle($data, $file) {
		$retval = sprintf("<?php\n".
                      "// Config cache file %s.ini\n".
                      "// date: %s\nreturn %s;\n",
                      $file, date('Y/m/d H:i:s'), var_export($data, true));

		return $retval;
	}
}